package bxgz.computer.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Random;

/**
 * Create by 肖祺彦
 * 2022/7/3
 */
@Controller
public class demo {

    //进入到首页
    @RequestMapping("/index")
    public String index() {

        return "index";
    }

    //进入到年级选择页
    @RequestMapping("/GradeChoose")
    public String GradeChoose(String username, String password) {
        if (username.equals("admin")&&password.equals("123")){
            return "GradeChoose";
        }
        else{
            return "index";
        }
    }

    @RequestMapping("/GradeChoose1")
    public String GradeChoose1(String choose){
        switch (choose) {
            case "一年级":
                return "FirstGradeChoose";
            case "二年级":
                return "SecondGradeChoose";
            case "三年级":
                return "ThirdGradeChoose";
            case "四年级":
                return "ForthGradeChoose";
            case "五年级":
                return "FifthGradeChoose";
            default:
                return "SixthGradeChoose";
        }
    }
    /**
     * int c=0;    //定义一个全局变量来存放整数计算结果
     * double d=0; //定义一个全局变量d来存放双精度类型的计算结果
     * int count1=0;   //定义一个全局变量count1来存放总共完成做题的次数
     * int count2=0;   //定义一个全局变量count2来存放答正确的次数
     * Random rd = new Random();   //实例化一个随机数对象
     * **/
    int c=0;
    double d=0;
    int count1=0;
    int count2=0;
    int score=0;
    Random rd = new Random();
    //一年级计算逻辑
    @RequestMapping("/FirstGrade")
    public String FirstGradeChoose(String choose,Model model){

        int b=rd.nextInt(20);
        int a=rd.nextInt(20)+b;
        if(choose.equals("加法")) {
            String Formula =a+"+"+b+"=?";
            model.addAttribute("Formula",Formula);
            //计算结果
            c=a+b;
            count1+=1;
        }else{
            String Formula =a+"-"+b+"=?";
            model.addAttribute("Formula",Formula);
            c=a-b;
            count1+=1;
        }
        return "Outcome1";
    }
    //二年级计算逻辑
    @RequestMapping("/SecondGrade")
    public String SecondGradeChoose(String choose,Model model){
        if(choose.equals("加法")) {

            int b=rd.nextInt(20);
            int a=rd.nextInt(20);
            String Formula =a+"+"+b+"=?";
            model.addAttribute("Formula",Formula);
            //计算结果
            c=a+b;
            count1+=1;
        }else if(choose.equals("乘法")){
            int a=rd.nextInt(10);
            int b=rd.nextInt(10);
            String Formula =a+"x"+b+"=?";
            model.addAttribute("Formula",Formula);
            c=a*b;
            count1+=1;
        }else{
            int b=rd.nextInt(20);
            int a=rd.nextInt(20)+b;
            String Formula =a+"-"+b+"=?";
            model.addAttribute("Formula",Formula);
            c=a-b;
            count1+=1;
        }
        return "Outcome2";
    }
    //三年级计算逻辑

    @RequestMapping("/ThirdGrade")
    public String ThirdGradeChoose(String choose,Model model){
        double a=rd.nextDouble()*10000;
        String x=String.format("%.2f",a);
        double n = Double.parseDouble(x);
        double b=rd.nextDouble()*10000;
        String y=String.format("%.2f",b);
        double m = Double.parseDouble(y);
        if(choose.equals("加法")) {
            String Formula=x+"+"+y+"=?";
            model.addAttribute("Formula",Formula);
            d=n+m;
            count1+=1;
        }else{
            String Formula=x+"-"+y+"=?";
            model.addAttribute("Formula",Formula);
            d=n-m;
            count1+=1;
        }
        return "Outcome3";
    }
    //四年级计算逻辑
    @RequestMapping("/ForthGrade")
    public String ForthGradeChoose(String choose,Model model){
        int a=rd.nextInt(90)+10;
        int b=rd.nextInt(90)+10;
        if(choose.equals("乘法")) {
            String Formula=a+"x"+b+"=?";
            model.addAttribute("Formula",Formula);
            c=a*b;
            count1+=1;
        }else{
            String Formula=a+"/"+b+"=?";
            model.addAttribute("Formula",Formula);
            d=(double)a/b;
            count1+=1;
        }
        return "Outcome4";
    }
    //五年级计算逻辑
    @RequestMapping("/FifthGrade")
    public String FifthGradeChoose(String choose,Model model){
        double a=rd.nextDouble()*10;
        String x=String.format("%.2f",a);
        double n = Double.parseDouble(x);
        double b=rd.nextDouble()*10;
        String y=String.format("%.2f",b);
        double m = Double.parseDouble(y);
        if(choose.equals("乘法")) {
            String Formula=x+"*"+y+"=?";
            model.addAttribute("Formula",Formula);
            d= n *m;
            String temp=String.format("%.2f",d);
            d= Double.parseDouble(temp);
            count1+=1;
        }else{
            String Formula=x+"/"+y+"=?";
            model.addAttribute("Formula",Formula);
            d= n /m;
            String temp=String.format("%.2f",d);
            d= Double.parseDouble(temp);
            count1+=1;
        }
        return "Outcome5";
    }
    //六年级计算逻辑
    @RequestMapping("/SixthGrade")
    public String SixGradeChoose(String choose,Model model){
        int a=rd.nextInt(10);
        int b=rd.nextInt(10);
        int e=rd.nextInt(10);
        if(choose.equals("一元一次方程加法")){
            String Formula=a+"x+"+b+"="+e;
            model.addAttribute("Formula",Formula);
            d=(double)(e-b)/(double)a;
            String temp=String.format("%.2f",d);
            d= Double.parseDouble(temp);
            count1+=1;
        }else{
            String Formula=a+"x-"+b+"="+e;
            model.addAttribute("Formula",Formula);
            d=(double)(e+b)/(double)a;
            String temp=String.format("%.2f",d);
            d= Double.parseDouble(temp);
            count1+=1;
        }
        return "Outcome6";
    }
    @RequestMapping("/Result1")
    public String Result1(Model model,String result) {
        //将前端传回的结果参数转变为数值
        //Integer.parseInt()可以将字符串类型的数字转换成数值型
        int result2=Integer.parseInt(result);
        if(c==result2) {
            model.addAttribute("msg", "恭喜你答对了!!!");
            count2+=1;
            score+=5;
        }else {
            model.addAttribute("msg", "很遗憾，答错了，继续努力");
            score-=5;
        }
        return "Result1";
    }
    @RequestMapping("/Result2")
    public String Result2(Model model,String result) {
        //将前端传回的结果参数转变为数值
        //Integer.parseInt()可以将字符串类型的数字转换成数值型
        int result2=Integer.parseInt(result);
        if(c==result2) {
            model.addAttribute("msg", "恭喜你答对了!!!");
            count2+=1;
            score+=5;
        }else {
            model.addAttribute("msg", "很遗憾，答错了，继续努力");
            score-=5;
        }
        return "Result2";
    }
    @RequestMapping("/Result3")
    public String Result3(Model model,String result) {
        //将前端传回的结果参数转变为数值
        //Integer.parseInt()可以将字符串类型的数字转换成数值型
        String result1 = String.valueOf(d);
        if(result1.equals(result)) {
            model.addAttribute("msg", "恭喜你答对了!!!");
            count2+=1;
            score+=5;
        }else {
            model.addAttribute("msg", "很遗憾，答错了，继续努力");
            score-=5;
        }
        return "Result3";
    }
    @RequestMapping("/Result4")
    public String Result4(Model model,String result) {
        //将前端传回的结果参数转变为数值
        //Integer.parseInt()可以将字符串类型的数字转换成数值型
//        int result2=Integer.parseInt(result);
        String result1 = String.format("%.2f",d);
        if(result1.equals(result)) {
            model.addAttribute("msg", "恭喜你答对了!!!");
            count2+=1;
            score+=5;
        }else {
            model.addAttribute("msg", "很遗憾，答错了，继续努力");
            score-=5;
        }
        return "Result4";
    }
    @RequestMapping("/Result5")
    public String Result5(Model model,String result) {
        //将前端传回的结果参数转变为数值
        //Integer.parseInt()可以将字符串类型的数字转换成数值型
//        int result2=Integer.parseInt(result);
        String result1 = String.format("%.2f",d);
        if(result1.equals(result)) {
            model.addAttribute("msg", "恭喜你答对了!!!");
            count2+=1;
            score+=5;
        }else {
            model.addAttribute("msg", "很遗憾，答错了，继续努力");
            score-=5;
        }
        return "Result5";
    }
    @RequestMapping("/Result6")
    public String Result6(Model model,String result) {
        //将前端传回的结果参数转变为数值
        //Integer.parseInt()可以将字符串类型的数字转换成数值型
//        int result2=Integer.parseInt(result);
        String result1 = String.format("%.2f",d);
        if(result1.equals(result)) {
            model.addAttribute("msg", "恭喜你答对了!!!");
            count2+=1;
            score+=5;
        }else {
            model.addAttribute("msg", "很遗憾，答错了，继续努力");
            score-=5;
        }
        return "Result6";
    }
    @RequestMapping("/Config1")
    public String Config1(String choose2,Model model) {
        if(choose2.equals("继续答题")) {
            return "FirstGradeChoose";
        }else {
            return GetEnd(model);
        }
    }
    @RequestMapping("/Config2")
    public String Config2(String choose2,Model model) {
        if(choose2.equals("继续答题")) {
            return "SecondGradeChoose";
        }else {
            return GetEnd(model);
        }
    }
    @RequestMapping("/Config3")
    public String Config3(String choose2,Model model) {
        if(choose2.equals("继续答题")) {
            return "ThirdGradeChoose";
        }else {
            return GetEnd(model);
        }
    }
    @RequestMapping("/Config4")
    public String Config4(String choose2,Model model) {
        if(choose2.equals("继续答题")) {
            return "ForthGradeChoose";
        }else {
            return GetEnd(model);
        }
    }
    @RequestMapping("/Config5")
    public String Config5(String choose2,Model model) {
        if(choose2.equals("继续答题")) {
            return "FifthGradeChoose";
        }else {
            return GetEnd(model);
        }
    }
    @RequestMapping("/Config6")
    public String Config6(String choose2,Model model) {
        if(choose2.equals("继续答题")) {
            return "SixthGradeChoose";
        }else {
            return GetEnd(model);
        }
    }
    //返回正确率的方法
    private String GetEnd(Model model) {
        model.addAttribute("Total",count1);                                 //答对的数量
        model.addAttribute("Correct",count2);                               //正确数
        double end = (double)count2/count1;
        double end1=end*100;
        String end2 = String.format("%.2f",end1);
        model.addAttribute("Accuracy",end2);   //正确率
        model.addAttribute("Score",score);                                  //得分
        return "EndPage";
    }

    @RequestMapping("/BackIndex")
    public String BackIndex(){
        return "index";
    }
}